﻿using AntdUI;
using AntdUI.In;
using BST250301TestStand.Models;
using BST250301TestStand.Models.Utils;
using MySql.Data.MySqlClient;
using Mysqlx.Crud;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Pqc.Crypto.Lms;
using ScottPlot.MultiplotLayouts;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using static System.ComponentModel.Design.ObjectSelectorEditor;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace BST250301.Views
{
    public partial class ProjectEditCount : UserControl
    {
        private AntdUI.Window window;
      
        private ProjectItem _item;
        private List<RecordData> _listRecord = new List<RecordData>();
        public bool submit;
        //string currentArea = string.Empty;
        public ProjectEditCount(AntdUI.Window win, ProjectItem item, bool isedit = true)
        {
            //tag = Target.GetInstance(this);
            this.window = win;
            if (item != null)
                this._item = item;
            InitializeComponent();
            GenerateCheckboxMatrix();
          
            InitData();
            if (!isedit)
            {
                this.buttonAdd.Enabled = false;
            }
            BindEventHandler();
            flowLayoutPanel1.FlowDirection = FlowDirection.LeftToRight;
            flowLayoutPanel1.WrapContents = true;
            flowLayoutPanel1.AutoScroll = true;
        }

        private void BindEventHandler()
        {
            

        }
        private void InitData()
        {
          
        }
        private void GenerateCheckboxMatrix()
        {
            _listRecord = DB.GetRecordData();
            for (int i = 0; i < _listRecord.Count; i++)
            {
                var checkbox = new System.Windows.Forms.CheckBox
                {
                    Name = $"Checkbox_{_listRecord[i].ID}",
                    Text = $"{_listRecord[i].Name}",  // 文本标签，例如 "CR 选项 1"
                    Size = new Size(300, 32),  // 固定大小
                    Margin = new Padding(5),
                    Tag = 100 + i  // 存储索引，便于事件中使用
                };
                // 添加选中事件（示例：打印选中状态）
                checkbox.CheckedChanged += (sender, e) =>
                {
                    var cb = sender as System.Windows.Forms.CheckBox;
                    Console.WriteLine($"Checkbox {cb.Tag} 选中: {cb.Checked}");
                };

                flowLayoutPanel1.Controls.Add(checkbox);
            }
            flowLayoutPanel1.PerformLayout();
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            bool isnew = false;
            DialogResult confirmResult = MessageBox.Show("确认修改记录", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (this._item == null)
            {
                isnew = true;
                this._item = new ProjectItem();
                this._item.RotateSpeed = new StepTest();
                this._item.CRSetPointCurrent = new StepTest();
                this._item.CFSetPointCurrent = new StepTest();
                this._item.CPTOSetPointCurrent = new StepTest();
                this._item.ItemType = ProType.数据统计;
                this._item.Name = inputName.Text;
                this._item.Description = inputDesc.Text;


                this._item.Code = Guid.NewGuid().ToString("N").Substring(0, 16);
                confirmResult = MessageBox.Show("确认增加记录", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            }
            if (confirmResult != DialogResult.Yes)
                return;
           
        }


        private void button2_Click(object sender, EventArgs e)
        {
            submit = false;
            this.Dispose();
        }

        private void buttonAddPara_Click(object sender, EventArgs e)
        {
            //var rp = this._item.ListRecord.First();
            var rp = new RecordData();
            var form = new RecordPara(rp);// { Size = new Size(500, 350) };
            AntdUI.Modal.open(new AntdUI.Modal.Config("", form, TType.None)
            {
                BtnHeight = 0,
            });
            if (form.submit)
            {
               
            }
        }
    }
}
